﻿using Sidvall.WorkItemManager.Data.Fields;
using System.Collections.Generic;
using System.Threading.Tasks;

namespace Sidvall.WorkItemManager.Model.Local.DataPortalManagers
{
    [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Naming", "CA1724:TypeNamesShouldNotMatchNamespaces")]
    public static partial class WorkItemManager
    {
        static partial void OnBeforeExecute(IDataPortalManager dataPortalManager, Sidvall.Business.DataPortalContext<Sidvall.Data.CommandCriteria, Sidvall.Data.CommandCriteria> dataPortalContext)
        {
            dataPortalContext.Tasks.Add(OnBeforeExecuteTask(dataPortalManager, dataPortalContext));
        }
        private static async Task OnBeforeExecuteTask(IDataPortalManager dataPortalManager, Sidvall.Business.DataPortalContext<Sidvall.Data.CommandCriteria, Sidvall.Data.CommandCriteria> dataPortalContext)
        {
            List<long> ids;
            long id;
            int iMax;

            switch (dataPortalContext.OperationContext.CriteriaId)
            {
                case ParameterManager.WorkItemCleanLogParameterName:
                case ParameterManager.WorkItemResetParameterName:
                    ids = new List<long>(dataPortalContext.OperationContext.Parameters.ValueToEnumerableInt64("WorkItemIds"));
                    if ((ids != null) && (ids.Count > 0))
                    {
                        iMax = ids.Count;
                        for (int i = iMax - 1; i >= 0; i--)
                        {
                            id = ids[i];
                            if (!await Sidvall.SystemContext.Current.AuthorizationManager.IsAuthorizedAsync(Sidvall.WorkItemManager.Data.Model.DomainObjects.WorkItem,
                                Sidvall.Security.Operations.Execute, dataPortalContext.OperationContext.CriteriaId, id.ToString()).ConfigureAwait(false))
                            {
                                ids.RemoveAt(i);
                            }
                        }
                        if (ids.Count == 0)
                            throw new Sidvall.Security.UnauthorizedException();
                    }
                    dataPortalContext.OperationContext.Parameters.Remove("WorkItemIds");
                    dataPortalContext.OperationContext.Parameters.Add("WorkItemIds", ids);
                    break;
                case ParameterManager.WorkItemLogParameterName:
                case ParameterManager.WorkItemUpdateParameterName:
                    id = dataPortalContext.OperationContext.Parameters.ValueToNullableInt64(WorkItemField.WorkItemId.ToString()).Value;
                    if (!await Sidvall.SystemContext.Current.AuthorizationManager.IsAuthorizedAsync(Sidvall.WorkItemManager.Data.Model.DomainObjects.WorkItem,
                        Sidvall.Security.Operations.Execute, dataPortalContext.OperationContext.CriteriaId, id.ToString()).ConfigureAwait(false))
                    {
                        throw new Sidvall.Security.UnauthorizedException();
                    }
                    break;
                default:
                    break;
            }
        }
    }
}